package ants.p2p.utils.net;

import ants.p2p.filesharing.WarriorAnt;
import java.io.*;

import org.apache.log4j.*;

public class UPnPDescriptor {

  static Logger _logger = Logger.getLogger(WarriorAnt.class.getName());

  public static String getUPnPDeviceDescriptor(){
    String decriptor = "<?xml version=\"1.0\" ?>\n"+
                       "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"+
                       "<specVersion>\n"+
                       "<major>1</major>\n"+
                       "<minor>0</minor>\n"+
                       "</specVersion>\n"+
                       "<device>\n"+
                       "<deviceType>urn:schemas-upnp-org:device:antsp2p:1</deviceType>\n"+
                       "<friendlyName>ANts P2P Device</friendlyName>\n"+
                       "<manufacturer>Gwren</manufacturer>\n"+
                       "<manufacturerURL>http://antsp2p.sourceforge.net</manufacturerURL>\n"+
                       "<modelDescription>ANts P2P Device</modelDescription>\n"+
                       "<modelName>ANtsP2P "+WarriorAnt.getVersion()+"["+WarriorAnt.getProtocolVersion()+"]</modelName>\n"+
                       "<modelNumber>"+WarriorAnt.getVersion()+"["+WarriorAnt.getProtocolVersion()+"]</modelNumber>\n"+
                       "<modelURL>http://antsp2p.sourceforge.net</modelURL>\n"+
                       "<serialNumber>"+System.currentTimeMillis()+"</serialNumber>\n"+
                       "<UDN>uuid:ANtsP2P"+System.currentTimeMillis()+"</UDN>\n"+
                       "<UPC>"+System.currentTimeMillis()+"</UPC>\n"+
                       "<iconList>\n"+
                       "<icon>\n"+
                       "<mimetype>image/gif</mimetype>\n"+
                       "<width>16</width>\n"+
                       "<height>16</height>\n"+
                       "<depth>8</depth>\n"+
                       "<url>"+_logger.getClass().getClassLoader().getResource("anims/ant_ico0.gif")+"</url>\n"+
                       "</icon>\n"+
                       "</iconList>\n"+
                       "<serviceList>\n"+
                       "<service>\n"+
                       "<serviceType>urn:schemas-upnp-org:service:address:1</serviceType>\n"+
                       "<serviceId>urn:schemas-upnp-org:serviceId:address:1</serviceId>\n"+
                       "<SCPDURL>/service/address/description.xml</SCPDURL>\n"+
                       "<controlURL>/service/address/control</controlURL>\n"+
                       "<eventSubURL>/service/address/eventSub</eventSubURL>\n"+
                       "</service>\n"+
                       "</serviceList>\n"+
                       "<presentationURL>http://antsp2p.sourceforge.net</presentationURL>\n"+
                       "</device>\n"+
                       "</root>\n";
    return decriptor;
  }

  public static String getUPnPServiceDescriptor(){
    String decriptor = "<?xml version=\"1.0\"?>\n"+
        "<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\n"+
        "<specVersion>\n"+
        "<major>1</major>\n"+
        "<minor>0</minor>\n"+
        "</specVersion>\n"+
        "<actionList>\n"+
        "<action>\n"+
        "<name>GetLANAddress</name>\n"+
        "<argumentList>\n"+
        "<argument>\n"+
        "<name>LANAddress</name>\n"+
        "<relatedStateVariable>LANAddress</relatedStateVariable>\n"+
        "<direction>out</direction>\n"+
        "</argument>\n"+
        "</argumentList>\n"+
        "</action>\n"+
        "</actionList>\n"+
        "<serviceStateTable>\n"+
        "<stateVariable sendEvents=\"yes\">\n"+
        "<name>LANAddress</name>\n"+
        "<dataType>string</dataType>\n"+
        "</stateVariable>\n"+
        "<stateVariable sendEvents=\"no\">\n"+
        "<name>Result</name>\n"+
        "<dataType>string</dataType>\n"+
        "</stateVariable>\n"+
        "</serviceStateTable>\n"+
        "</scpd>";
    return decriptor;
  }

  public static void createDirectoryStructure(){
     File upnpDescriptorDir = new File(WarriorAnt.workingPath+"upnpdescriptor/");
     if(!upnpDescriptorDir.exists()){
       upnpDescriptorDir.mkdir();
     }


     File upnpDescriptor = new File(WarriorAnt.workingPath+"upnpdescriptor/description.xml");
     try{
       PrintWriter pw = new PrintWriter(upnpDescriptor);
       pw.print(getUPnPDeviceDescriptor());
       pw.close();
     }catch(IOException e){
       _logger.error("Cannot create UPnP Device descriptor", e);
     }
     File upnpServiceDescriptorDir = new File(WarriorAnt.workingPath+"upnpdescriptor/service/");
     if(!upnpServiceDescriptorDir.exists()){
       upnpServiceDescriptorDir.mkdir();
     }
     File upnpLANAddressDescriptorDir = new File(WarriorAnt.workingPath+"upnpdescriptor/service/address");
     if(!upnpLANAddressDescriptorDir.exists()){
       upnpLANAddressDescriptorDir.mkdir();
     }
     File upnpServiceDescriptor = new File(WarriorAnt.workingPath+"upnpdescriptor/service/address/description.xml");
     try{
       PrintWriter pw = new PrintWriter(upnpServiceDescriptor);
       pw.print(getUPnPServiceDescriptor());
       pw.close();
     }catch(IOException e){
       _logger.error("Cannot create UPnP Service descriptor", e);
     }
  }
}
